//
//  MainTabBarController.m
//  MiAiApp
//
//  Created by 徐阳 on 2017/5/18.
//  Copyright © 2017年 徐阳. All rights reserved.
//

#import "MainTabBarController.h"
#import "RootNavigationController.h"
#import "UITabBar+CustomBadge.h"
#import "XYTabBar.h"
#import "SJMeVC.h"
#import "SJHomeVC.h"
#import "SJMyApplyDetailVC.h"

@interface MainTabBarController ()<UITabBarControllerDelegate>

@property (nonatomic,strong) NSMutableArray * VCS;//tabbar root VC

@end

@implementation MainTabBarController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    //初始化tabbar
    [self setUpTabBar];
    //添加子控制器
    [self setUpAllChildViewController];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}


#pragma mark ————— 初始化TabBar —————
-(void)setUpTabBar{
    //设置背景色 去掉分割线
    [self setValue:[XYTabBar new] forKey:@"tabBar"];
    [self.tabBar setBackgroundColor:[UIColor whiteColor]];
    [self.tabBar setBackgroundImage:[UIImage new]];
    //通过这两个参数来调整badge位置
    //    [self.tabBar setTabIconWidth:29];
    //    [self.tabBar setBadgeTop:9];
}

#pragma mark - ——————— 初始化VC ————————
-(void)setUpAllChildViewController{
    
    _VCS = @[].mutableCopy;
    
    
    NSString *key = [NSString stringWithFormat:@"curOrder_%@",kUserInfo.userId];
//    [[NSUserDefaults standardUserDefaults] setObject:@{} forKey:key];
//    [[NSUserDefaults standardUserDefaults] synchronize];
    NSDictionary *applyInfo = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    
    NSString *orderId = @"";
    if ([applyInfo isKindOfClass:[NSDictionary class]]) {
        orderId = kSTRING(applyInfo[@"id"]);
    }
    RootViewController *homeVc;
    if (orderId.length > 0) {
        homeVc = [[SJMyApplyDetailVC alloc] init];
    }else{
        homeVc = [[SJHomeVC alloc] init];
    }
    [self setupChildViewController:homeVc title:kLocalString(@"Home") imageName:@"tabbar_home_nor" seleceImageName:@"tabbar_home_sel"];

    SJMeVC *meVc = [[SJMeVC alloc]init];
    [self setupChildViewController:meVc title:kLocalString(@"Me") imageName:@"tabbar_me_nor" seleceImageName:@"tabbar_me_sel"];
   
    
    self.viewControllers = _VCS;
}

-(void)setupChildViewController:(UIViewController*)controller title:(NSString *)title imageName:(NSString *)imageName seleceImageName:(NSString *)selectImageName{
    controller.title = title;
    controller.tabBarItem.title = title;//跟上面一样效果
    controller.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    controller.tabBarItem.selectedImage = [[UIImage imageNamed:selectImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    if (@available(iOS 13.0, *)){
        UITabBar *tabBar = [UITabBar appearance];
        [tabBar setTintColor:[UIColor colorWithHexString:@"#4D94FF"]];
        [tabBar setUnselectedItemTintColor:[UIColor colorWithHexString:@"#000000"]];
    }else{
        //未选中字体颜色
        [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#000000"],NSFontAttributeName:kFont(12)} forState:UIControlStateNormal];
        //选中字体颜色
        [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#4D94FF"],NSFontAttributeName:kFont(12)} forState:UIControlStateSelected];
    }

    //包装导航控制器
    RootNavigationController *nav = [[RootNavigationController alloc] initWithRootViewController:controller];
    
    [_VCS addObject:nav];
}



-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    
}

-(void)setRedDotWithIndex:(NSInteger)index isShow:(BOOL)isShow{
    if (isShow) {
        [self.tabBar setBadgeStyle:kCustomBadgeStyleRedDot value:0 atIndex:index];
    }else{
        [self.tabBar setBadgeStyle:kCustomBadgeStyleNone value:0 atIndex:index];
    }
    
}

- (BOOL)shouldAutorotate {
    return [self.selectedViewController shouldAutorotate];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return [self.selectedViewController supportedInterfaceOrientations];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
